<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class used as translator for phrases
 * @author Nicola Dinh
 * @version 2011.04.2312
 */
class phrase {

  /*
   * All properties
   */
  private static $phrases = array();
  private static $langs = array();

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Load phrases & initialise
   */
  public static function initialise() {
    self::$langs = config::get_supported_langs();
    if (data::file_exists("phrases"))
      self::$phrases = data::load_from_file("phrases"); 
  }
  
  /**
   * Save all phrases back to data file
   */
  public static function save_all() {
    data::save_to_file("phrases",self::$phrases);
  }
  
  /**
   * Get all loaded phrases
   * @return array The multi-level array containing all loaded phrases
   */
  public static function get_loaded_phrases() {
    return self::$phrases;
  }
  
  /**
   * Set a translation for certain phrase
   * @param string $phrase_uk The english phrase
   * @param string $lang The language of translated phrase
   * @param string $phrase_other The phrase in $lang language
   */
  public static function set_phrase($phrase_uk,$lang,$phrase_other) {
    $phrase_uk = trim($phrase_uk);
    $lang = trim($lang);
    $phrase_other = trim($phrase_other);
    $langs = self::$langs;    
    
    //add if not existing
    if (!isset(self::$phrases[$phrase_uk]))
      self::$phrases[$phrase_uk] = array();
    foreach ($langs as $abbreviation=>$lang_name) 
      if (!isset(self::$phrases[$phrase_uk][$abbreviation])) 
        self::$phrases[$phrase_uk][$abbreviation] = $phrase_uk;
        
    //set translated phrase
    self::$phrases[$phrase_uk][$lang] = $phrase_other;
  }
  
  /**
   * Translate into current language
   * @param string $text The text to translate
   * @return string The text translated into current language
   */
  public static function __($text) {
    $langs = self::$langs;
    $cur_lang = helper::get_cur_lang();   
    
    //add phrase if in devmode & not exists
    if (system::$devmode) {
      if (!isset(self::$phrases[$text]))
        self::$phrases[$text] = array();
      foreach ($langs as $abbreviation=>$lang) 
        if (!isset(self::$phrases[$text][$abbreviation])) 
          self::$phrases[$text][$abbreviation] = "???";
    }
  
    //check if phrase exists in data file
    if (!isset(self::$phrases[$text])) 
      return $text;
      
    //check if the phrase exists in current language
    if (!isset(self::$phrases[$text][$cur_lang])) 
      return $text;
    
    //found matching phrase in current language
    $phrase = trim(self::$phrases[$text][$cur_lang]); 
    if (strlen($phrase)>0 && $phrase!="???")
      return $phrase;
    else
      return $text;    
  }
}